﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KOPLibrary.Forms
{
    public partial class ProgressForm : Form
    {
        public event EventHandler Cancel;
        private Control owner = null;
        private bool canceled = false;




        public bool Canceled
        {
            get
            {
                Application.DoEvents();
                return canceled;
            }
            private set
            {
                canceled = value;
            }
        }

        public int Current { get; private set; }

        public ProgressForm()
        {
            InitializeComponent();
        }

        internal void Initialize(int min, int max, string msg, bool canCancel, Control owner)
        {
            pBar.Minimum = min;
            pBar.Maximum = max;
            pBar.Value = min;
            Current = min;
            ctlMsg.Text = msg;
            ctlCancel.Visible = canCancel;
            if (owner != null)
                owner.Enabled = false;
            this.owner = owner;
            Show();
            Refresh();
        }

        internal void Done()
        {
            Close();
            if (owner != null)
            owner.Enabled = true;
        }

        internal void Update(string msg)
        {
            ctlMsg.Text = msg;
            pBar.Increment(1);
            Current++;
            Refresh();
            BringToFront();           
            Application.DoEvents();
        }

        internal void SetMessage(string msg)
        {
            ctlMsg.Text = msg;
            Refresh();
            BringToFront();
            Application.DoEvents();
        }

        private void ctlCancel_Click(object sender, EventArgs e)
        {
            if (Util.Confirm("İşlem iptal edilsin mi ?"))
            {
                Canceled = true;
                if (Cancel != null)
                    Cancel(this, EventArgs.Empty);
                Done();
            }
        }
    }
}
